package com.donleo.ssm.controller;

import com.donleo.ssm.model.CodeModel;
import com.donleo.ssm.service.ICodeService;
import com.donleo.ssm.utils.ResponseLayResult;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author liangd
 * date 2020-11-24 10:42
 * code
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Resource
    private ICodeService codeService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public ResponseLayResult add(@RequestBody CodeModel codeModel) {
        Integer count = codeService.add(codeModel);
        return ResponseLayResult.createBySuccess(count, count);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseLayResult delete(Integer id) {
        Integer count = codeService.delete(id);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deletes")
    public ResponseLayResult deletes(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        Integer count = codeService.deletes(idList);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 修改
     *
     * @param codeModel
     * @return
     */
    @PostMapping("/update")
    public ResponseLayResult update(@RequestBody CodeModel codeModel) {
        Assert.notNull(codeModel.getId(), "Id is not null");
        Integer count = codeService.update(codeModel);
        return ResponseLayResult.createBySuccess(count, count);
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseLayResult findById(Integer id) {
        CodeModel codeModel = codeService.findById(id);
        return ResponseLayResult.createBySuccess(1, codeModel);
    }

    /**
     * 查询所有
     *
     * @param codeModel
     * @return
     */
    @GetMapping("/findAll")
    public ResponseLayResult findAll(CodeModel codeModel) {
        List<CodeModel> list = codeService.findAll(codeModel);
        Integer count = codeService.getCount(codeModel);
        return ResponseLayResult.createBySuccess(count, list);
    }

    /**
     * 根据类型查询码表
     * @param type
     * @return
     */
    @GetMapping("/findByType")
    public ResponseLayResult findByType(String type){
        List<CodeModel> list = codeService.findByType(type);
        return ResponseLayResult.createBySuccess(200,list);
    }
}
